<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The Association_At class for table 'association_mark'.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.1
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/classes/Memcached_DataObject.php';


class Association_Mark extends Memcached_DataObject
{
	public $__table = 'association_mark';	// table name
	public $id;								// int(11)  primary_key not_null
	public $user_id;						// int(11)  foreign_key not_null
	public $marktimes;						// int(11)  default_1
	public $average;						// int(11)  default_30
	
	// Static get
	function staticGet($key, $value = null)
	{
		return Memcached_DataObject::staticGet('Association_Mark', $key, $value); 
	}
	
	/**
	 * Mark the association.
	 */
	function mark($mark)
	{
		// Mark!
		$orgiAssociation_mark = clone($this);

		$this->average = intval(round(($this->average * $this->marktimes + $mark) / ++$this->marktimes));
		
		if ($this->update($orgiAssociation_mark)) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Get the average mark. Divide by 10.
	 */
	static function getAverage($user_id)
	{
		$association_work = Association_Mark::staticGet('user_id', $user_id);
		if (empty($association_work)) {
			return 3;
		}
		return number_format($association_work->average / 10, 1);
	}
}
?>